<?php

namespace App\Http\Controllers\Admin\Category;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CategoryController extends Controller
{
    //分类列表的展示
    public function getList()
    {
    	//使用左连接查询父分类的名字
    	$data = DB::table('category as c1') 
    			->select('c1.*',DB::raw("concat(c1.path, ',', c1.id) as path_name"), 'c2.name as pname')
    			-> leftJoin('category as c2', 'c1.pid', '=', 'c2.id')
    			-> orderBy('path_name')
    			-> paginate(5);
    	foreach($data as $k => $v)
    	{
    		$num = substr_count($v -> path, ',');
    		$str = str_repeat('|----', $num);
    		$data[$k] -> name =  $str. $v -> name;
    	}
    	return view('admin.category.list',['title' => '分类列表','data' => $data]);
    }

    //添加分类展示
    public function getAdd()
    {
    	$data = DB::table('category')
    			-> select('*',DB::raw("concat(path, ',', id) as new_path"))
    			-> orderBy('new_path')
    			-> get();
    	//设置分类之间的关系 层级显示
    	foreach($data as $k => $v)
    	{
    		//获得由此字符串分隔的次数
    		$num = substr_count($v -> path, ',');
    		//重复某个字符串n次
    		$str = str_repeat('|----', $num);
    		//将数组中对象的值 拼接好
    		$data[$k] -> name =  $str. $v -> name;
    	}

    	return view('admin.category.add',['title' => '分类添加','data' => $data]);
    }
    //分类添加实现
    public function postInsert(Request $request)
    {
    	$this->validate($request, [
		    'name' => 'required',
		],[
			'name.required' => '分类名不能为空',
		]);

    	$data = $request -> except('_token');
    	if(!$data['pid'] == 0)
    	{
	    	$parent_path = DB::table('category') -> where('id', $data['pid']) -> first() -> path;
    		$data['path'] = $parent_path . ',' . $data['pid'];
    		$res = DB::table('category') -> insert($data);
    		if($res)
    		{
    			return redirect('admin/category/list') -> with(['info' => '添加成功']);
    		}else
    		{
    			return back() -> with(['info' => '添加失败']);
    		}	    	
    	}else
    	{
    		$data['pid'] = 0;
    		$data['path'] = 0;
    		$res = DB::table('category') -> insert($data);
    		if($res)
    		{
    			return redirect('admin/category/list') -> with(['info' => '添加成功']);
    		}else
    		{
    			return back() -> with(['info' => '添加失败']);
    		}	    	
    	}

    }
    //分类编辑页面展示
    public function getEdit($id)
    {

    	$data = DB::table('category') -> where('id', $id) -> first();
    	$allData = DB::table('category') -> get();
    	foreach($allData as $k => $v)
    	{
    		//获得由此字符串分隔的次数
    		$num = substr_count($v -> path, ',');
    		//重复某个字符串n次
    		$str = str_repeat('|----', $num);
    		//将数组中对象的值 拼接好
    		$allData[$k] -> name =  $str. $v -> name;
    	}

    	return view('admin.category.edit',['title' => '分类编辑','data' => $data,'allData' => $allData]);
    }
    //分类的编辑
    public function postUpdate(Request $request, $id)
    {
    	// dd($request -> all());
    	$data = $request -> except('_token');
    	if(!$data['pid'] == 0)
    	{
	    	$parent_path = DB::table('category') -> where('id', $data['pid']) -> first() -> path;
    		$data['path'] = $parent_path . ',' . $data['pid'];
    		$res = DB::table('category') -> where('id', $id) ->update($data);
    		if($res)
    		{
    			return redirect('admin/category/list') -> with(['info' => '编辑成功']);
    		}else
    		{
    			return back() -> with(['info' => '编辑失败']);
    		}    		
    	}else
    	{
    		$data['pid'] = 0;
    		$data['path'] = 0;
    		$res = DB::table('category') -> where('id', $id) -> update($data);
    		if($res)
    		{
    			return redirect('admin/category/list') -> with(['info' => '编辑成功']);
    		}else
    		{
    			return back() -> with(['info' => '编辑失败']);
    		}	    	
    	}
    }
    public function getDelete($id)
    {
    	$data = DB::table('category') -> where('pid', $id) -> get();
    	if(!$data)
    	{
    		$res = DB::table('category') -> where('id', $id) -> delete();
    		if($res)
    		{
    			return redirect('admin/category/list') -> with(['info' => '删除成功']);
    		}else
    		{
    			return back() -> with(['info' => '删除失败']);
    		}
    	}else
    	{
    		return back() -> with(['info' => '存在子分类不能删除']);
    	}
    }
}
